#pragma once
#include <iostream>
#include "Lock.hpp"
#include <filesystem>
#include <fstream>
#include <unistd.h>
#include <sys/types.h>
#include <memory>
class LogStrategy
{
public:
    LogStrategy() {}
    ~LogStrategy() {}
    virtual void Strategy(std::string messager) = 0;

private:
};

// 向显示器上打印：
class ConsoleLogStrategy : public LogStrategy
{
public:
    ~ConsoleLogStrategy() {}
    ConsoleLogStrategy() {}
    void Strategy(std::string messager) override
    {
        LockGuard lock(_mutex);
        std::cout << messager << std::endl;
    }

private:
    Mutex _mutex;
};

// 向指定文件中打印：
const std::string gsep = "/r/n";
const std::string default_path="./";
const std::string default_file="my.log";
class FileLogStrategy : public LogStrategy
{
public:
    FileLogStrategy(std::string path=default_path, std::string file=default_file)
        : _path(path), _file(file)
    {
        _file_name = _path + (_path.back() == '/' ? "" : "/") + _file;
        if (std::filesystem::exists(_path))
        {
            return;
        }
        std::filesystem::create_directories(_path);
    }
    ~FileLogStrategy()
    {
    }
    void Strategy(std::string messager) override
    {
        LockGuard lock(_mutex);
        std::ofstream out(_file_name, std::ios::app);
        out << messager << gsep;
        out.close();
    }

private:
    std::string _path;
    std::string _file;
    std::string _file_name;
    Mutex _mutex;
};

enum class LogLevel
{
    DEBUG,
    INFO,
    WARNING,
    ERROR,
    FATAL
};
std::string GetTime()
{
    time_t curr = time(nullptr);
    struct tm curr_tm;
    localtime_r(&curr, &curr_tm);
    char timebuffer[128];
    snprintf(timebuffer, sizeof(timebuffer), "%4d-%02d-%02d %02d:%02d:%02d",
             curr_tm.tm_year + 1900,
             curr_tm.tm_mon + 1,
             curr_tm.tm_mday,
             curr_tm.tm_hour,
             curr_tm.tm_min,
             curr_tm.tm_sec);
    return timebuffer;
}
std::string GetLevel(LogLevel level)
{
    switch (level)
    {
    case LogLevel::DEBUG:
        return "DEBGE";
    case LogLevel::INFO:
        return "INFO";
    case LogLevel::WARNING:
        return "WARNING";
    case LogLevel::ERROR:
        return "ERROR";
    case LogLevel::FATAL:
        return "FATAL";
    default:
        return "None";
    }
}

class Logger
{
public:
    Logger()
    {
        // 向显示屏打印：
        _flush_Strategy = std::make_unique<ConsoleLogStrategy>();
    }
    ~Logger()
    {
    }
    class logmessager
    {
    public:
        logmessager(LogLevel &level, std::string src_file, int line, Logger &logger)
            : _time(GetTime()), _level(GetLevel(level)), _id(getpid()), _src_file(src_file), _line(line), _logg(logger)
        {
            std::stringstream ss;
            ss << "[" << _time << "] "
               << "[" << _level << "] "
               << "[" << _id << "] "
               << "[" << _src_file << "] "
               << "[" << _line << "] "
               << "- ";
            _log_info = ss.str();
        }
        logmessager &operator<<(const std::string info)
        {
            _log_info += info;
            return *this;
        }
        ~logmessager()
        {
            _logg._flush_Strategy->Strategy(_log_info);
        }

    private:
        std::string _time;
        std::string _level;
        pid_t _id;
        std::string _src_file;
        int _line;
        Logger &_logg;
        std::string _log_info;
    };
    logmessager operator()(LogLevel level, std::string file, int line)
    {
        return logmessager(level, file, line, *this);
    }

private:
    std::unique_ptr<LogStrategy> _flush_Strategy;
};


Logger logger;
#define LOG(level) logger(level, __FILE__, __LINE__)